<?php

require('common.php');

// list all of the fields in this table as an ordered list
// provide links to drop the fields
function desc($table)
{
	$result = sql_exec("SHOW COLUMNS FROM $table");
	if (mysql_num_rows($result) > 0)
	{
		echo "<ol>\n";
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
			$field_name = $row['Field'];
			switch ($row['Type']) {
			case 'int(11)':
				$type = "Integer";
				break;
			case 'text':
				$type = "Text";
				break;
			}
			
			$delete_uri = "edit_table.php?table=$table&delete=$field_name";
			$delete_link = '<a href="' . $delete_uri . '">Delete</a>';
			
			$required = ($row['Null'] == "NO") ? "Cannot be Blank" : "May be Blank";
		
			echo "<li>$field_name ($type) ($required)    $delete_link</li>\n";
		}
		echo "</ol>\n";
	}
}

if (!empty($G['new_field']))
{
	$field = $G['new_field'];
	$table = $G['table'];
	$type = $G['type'];
	$null = $G['required'];

	if ($null != 'NOT NULL') 
	{
		$null = 'NULL';
	}

	sql_exec("ALTER TABLE $table ADD COLUMN $field $type $null ");
}

if (!empty($G['delete']))
{
	$table = $G['table'];
	$result = sql_exec("SHOW COLUMNS FROM $table");
	if (mysql_num_rows($result) == 1)
	{
		sql_exec("DROP TABLE $table");
		header("Location: index.php");
	} else {
		$delete = $G['delete'];
		sql_exec("ALTER TABLE $table DROP COLUMN $delete");
	}
}



html_header("edit table");
?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<h4>edit table</h4>

<p><?php desc($G['table']); ?></p>

<form action="edit_table.php" method="get">

<p>Field Name: <input name="new_field" /></p>

<p>Data Type: <select name="type">
<option value="INT">Integer</option>
<option value="TEXT">Text</option>
</select>
</p>

<p>Required: <input type="checkbox" name="required" value="NOT NULL" /></p>

<p><input type="submit" value="save" /></p>

<input type="hidden" name="table" value="<?php echo $G['table'] ?>" />

</form>

<p><a href="index.php">done</a></p>

<?php echo footer(); ?>

</div>

</div>
</body>
</html>
